package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录拦截部分请求 LoginInterceptor
        registry.addInterceptor(new LoginInterceptor()).excludePathPatterns(
                //拦截不包含路径  放行的
                "/shop/**",
                "/voucher/**",
                "/shop-type/**",
                "/blog/hot",
                //上传方便测试
                "/upload/**",
                "/user/code",
                "/user/login"
        ).order(1);  //order执行优先级   默认零优先  1往后

        //添加拦截所有请求 RefreshTokenInterceptor   token刷新拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);

        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
